/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"sasuke/pw_coding"
)

var (
	// 语言标识
	lang string
	// 生成类型
	gentype string
	// 关键字/数据库名
	code string
	// Ip
	ip string
	// 端口
	port string
	// 用户
	user string
	// 密码
	pwd string
)

// genCmd represents the gen command
var genCmd = &cobra.Command{
	Use:   "gen",
	Short: "Language Template Code Gen",
	Long: `
	C#:
		1. 生成单Class文件命令：gen -l cs -c [YourClassName]
		2. 生成Csproj项目命令：gen -l cs -t m -c [YourModuleName]
	Go:
		1. 生成单go文件命令：gen -l go -c [YourStructName]
		2. 通过数据库反射生成模块项目命令：gen -l go -t m -c [DbName] -i [Ip] -r [port] -u [user] -p [password]
	`,
	Run: func(cmd *cobra.Command, args []string) {
		var g pw_coding.Gener
		if lang == "go" {
			g = new(pw_coding.GoGener)
		} else {
			g = new(pw_coding.CsGener)
		}
		g.Coding(&pw_coding.CmdParam{
			Language: lang,
			GenType:  gentype,
			Code:     code,
			Ip:       ip,
			Port:     port,
			User:     user,
			Pwd:      pwd,
		})
		fmt.Println("create completed ...")
	},
}

func init() {
	rootCmd.AddCommand(genCmd)
	genCmd.Flags().StringVarP(&lang, "lang", "l", "cs", "language CSharp=>cs Golang=>go")
	genCmd.Flags().StringVarP(&gentype, "gentype", "t", "m", "f=单个文件 m=module(CS:csproj) d(从数据库映射)")
	genCmd.Flags().StringVarP(&code, "code", "c", "Test", "CodeName/Dbname (required) ")
	genCmd.Flags().StringVarP(&ip, "ip", "i", "localhost", "db ip localhost")
	genCmd.Flags().StringVarP(&port, "port", "r", "3306", "db port 3306")
	genCmd.Flags().StringVarP(&user, "user", "u", "root", "db user root")
	genCmd.Flags().StringVarP(&pwd, "pwd", "p", "123456", "db password 123456")
	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// genCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// genCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
